package com.wanghaibo.vgashow.climatecontrol;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.VideoView;

import com.wanghaibo.vgashow.R;

//import com.vite.audiolibrary.FmlPlayer;


public class SwingFlapBgView extends FrameLayout {

    private VideoView videoBg;
    private View solidBg;

    private Context mContext;
    //    private FmlPlayer fmlPlayer;
    private MediaPlayer musicPlayer;

    public SwingFlapBgView(Context context) {
        super(context);
        mContext = context;
        View inflate = inflate(getContext(), R.layout.view_swing_flap_bg, this);
    }

    public SwingFlapBgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View inflate = inflate(getContext(), R.layout.view_swing_flap_bg, this);
    }

    public SwingFlapBgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        View inflate = inflate(getContext(), R.layout.view_swing_flap_bg, this);
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        //我们重新计算高度
//        int width = getDefaultSize(0, widthMeasureSpec);
//        int height = getDefaultSize(0, heightMeasureSpec);
//        setMeasuredDimension(width, height);
//    }

    public void init(Uri videoUri, int musicResId) {
        videoBg = (VideoView) findViewById(R.id.video_bg);
        solidBg = (View) findViewById(R.id.solid_bg);

        //视频资源
        videoBg.setVideoURI(videoUri);
        videoBg.setVisibility(GONE);

//        //初始化FmlPlayer
//        FmlPlayer.init(mContext, true);
//        fmlPlayer = new FmlPlayer();
//        //音频资源
//        fmlPlayer.setAssetFile(musicFile);
//        fmlPlayer.prepare();
        musicPlayer = MediaPlayer.create(mContext,musicResId);
        musicPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

    public void startPlay() {
        videoBg.setVisibility(VISIBLE);
        //视频播放
        videoBg.start();
        //视频循环播放
        videoBg.setOnPreparedListener(mp -> {
            mp.setLooping(true);
        });

        //纯色背景alpha渐变
        ObjectAnimator animator = ObjectAnimator.ofFloat(solidBg, "alpha", solidBg.getAlpha(), 0.0f)
                .setDuration(1000);
        animator.setAutoCancel(true);
        animator.start();

        //音频播放
        //循环播放
        musicPlayer.setLooping(true);
        musicPlayer.start();
//        //音频播放
//        //循环播放
//        fmlPlayer.setLooping(true);
//        fmlPlayer.play();


    }

    public void stopPlay() {
        //纯色背景alpha渐变
        ObjectAnimator animator = ObjectAnimator.ofFloat(solidBg, "alpha", solidBg.getAlpha(), 1.0f)
                .setDuration(500);
        animator.setAutoCancel(true);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //视频停止
                videoBg.stopPlayback();
                videoBg.setVisibility(View.GONE);
                //音乐停止
                musicPlayer.stop();
//                //音乐停止
//                fmlPlayer.stop();
            }
        });
    }

    public void releaseResource() {
        musicPlayer.release();
//        FmlPlayer.free();
    }
}
